using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Infrastructure.Tools;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
/// <summary>
/// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
/// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
/// </summary>
	internal class SysCustomersEF
	{
		public SysCustomersEF(DbContext dbContext)
		{
			sysCustomersServerEF = new SysCustomersServerEF(dbContext);
			sysAccountsServerEF = new SysAccountsServerEF(dbContext);
		}
		public SysCustomersServerEF sysCustomersServerEF { get; set; }
        public SysAccountsServerEF sysAccountsServerEF { get; set; }
    }
	[InjectClass]
	public class SysCustomersServer : ISysCustomersServer
	{
		private readonly SysCustomersEF _sysCustomersEF;//包含所有所需仓库表实现的对象
		// 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
		public SysCustomersServer(DbContext dbContext)
		{
			_sysCustomersEF = new SysCustomersEF(dbContext);
		}
		public async Task<ResponseApiDto> RegisterAsync(RegisterCustomerDto dto)
		{
			if(await _sysCustomersEF.sysCustomersServerEF.Query(d => d.CPhoneNumber == dto.phone).AnyAsync())
			{
				return ResponseApiDto.Fail("账号已注册");
			}
			int affectedRows = 0;
			await _sysCustomersEF.sysCustomersServerEF.ExecuteInTransactionAsync(async() => {
                string salt = Tools.Guid_1();
                SysAccounts account = new SysAccounts()
                {
                    AAccount = dto.phone,
                    APassword = Encrypt.GetMd5Hash(dto.password+salt),
                    AName = "小白",
                    ARole = "user",
                    ASalt = salt,
                };
				affectedRows+=await _sysCustomersEF.sysAccountsServerEF.AddAsync(account);
                SysCustomers customer = new SysCustomers()
                {
					CAccount=account.AId,
					CEmail="",
					CPhoneNumber=dto.phone
                };
				affectedRows += await _sysCustomersEF.sysCustomersServerEF.AddAsync(customer);
            }, () =>
			{
				affectedRows = 0;return null;
			});
			return ResponseApiDto.Create(affectedRows, "注册成功", "注册失败");
		}
		public async Task<ResponseApiDto> DeleteAsync(RequestSysCustomersDto dto)
		{
			return ResponseApiDto.Create(await _sysCustomersEF.sysCustomersServerEF.DeleteAsync(dto.ToDeleteEntity()));
		}
		public async Task<ResponseApiDto> UpdateAsync(RequestSysCustomersDto dto)
		{
			return ResponseApiDto.Create(await _sysCustomersEF.sysCustomersServerEF.UpdateAllProperNotNullAsync(dto.ToUpdateEntity()));
		}
		public async Task<ResponseApiDto> PaginationAsync(int page, int limit)
		{
			int total = 0;
			List<ResponseSysCustomersDto> data = new List<ResponseSysCustomersDto>();
			List<SysCustomers> sysCustomerss = await _sysCustomersEF.sysCustomersServerEF.Pagination1(d=>true,false, d => d.CCreatedAt, out total, page, limit).ToListAsync();
			foreach (var sysCustomers in sysCustomerss)
			{
				data.Add(sysCustomers.ToResponseDto());
			}
			return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total=total,data=data});
		}

        public async Task<ResponseApiDto> GetCustomerByAccount(string aid)
        {
			var customer=await _sysCustomersEF.sysCustomersServerEF.Query(d => d.CAccount == aid).SingleAsync();
			return ResponseApiDto.Success("数据已获取",customer.ToResponseDto());
        }
    }
}
